package com.be.beadmin.project.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.PayInfoDto;
import com.be.beadmin.project.dto.output.PayInfoEx;
import com.be.beadmin.project.service.PayInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-06-14
**/
@Api(tags = "项目付款信息")
@RestController
public class PayInfoController {

    @Autowired
    private  PayInfoService payInfoService;



    @ApiOperation("根据获取项目Id获取付款信息")
    @PostMapping(value = "/project/getPayInfoByInfoId")
    public ResultModel getPayInfoByInfoId(@RequestParam String infoId) {
        return ResultModel.success(payInfoService.getPayInfoByInfoId(infoId));
    }



    @PostMapping(value = "/project/getPayInfoByPage")
    public ResultModel getPayInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(payInfoService.getPayInfoByPage(pageEntity)) ;
    }

    @PostMapping("/project/addPayInfo")
    public ResultModel addPayInfo(@RequestBody PayInfoDto payInfoDto){
        payInfoService.addPayInfo(payInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/project/delPayInfo")
    public ResultModel delPayInfo(@RequestBody PayInfoDto payInfoDto){
        payInfoService.delPayInfo(payInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("更新付款信息")
    @PostMapping("/project/updatePayInfo")
    public ResultModel updatePayInfo(@RequestBody PayInfoDto payInfoDto){
        payInfoService.updatePayInfo(payInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/project/exportPayInfo")
    public void exportPayInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PayInfoEx> payInfoPage = payInfoService.getPayInfoByPage(pageEntity);
        List<PayInfoEx> payInfoExes = payInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PayInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PayInfoEx.class).sheet("sheet1").doWrite(payInfoExes);
    }
}